using System;
using System.Text;
using System.Reflection;
using System.IO;
using Gadgeteer.Modules.Seeed;
using GeoBox.BoxApplication.CellularRadioDriver;
using System.Threading;
using GeoBox.BoxApplication.JSON;

namespace GeoBox.BoxApplication
{
    public class AzureClient
    {
        public bool IsReady { get { return _cellularRadio.IsReady; } }
        private readonly string _userAgent;
        private readonly string _baseAddress;
        private readonly CellularRadioManager _cellularRadio;

        public AzureClient(CellularRadioManager cellularRadio)
            : this("gpsbox.azurewebsites.net/api", cellularRadio)
        {
        }

        public AzureClient(string baseAddress, CellularRadioManager cellularRadio)
        {
            _userAgent = "GeoBoxV" + Assembly.GetExecutingAssembly().GetName().Version;
            _baseAddress = baseAddress;

            _cellularRadio = cellularRadio;
            _cellularRadio.AccessPointName = "bob.at";
            _cellularRadio.AccessPointUser = "data@bob.at";
            _cellularRadio.AccessPointPassword = "ppp";
        }


        public bool PostJson(string subUri, string json)
        {
            Debug.Print("PostJson was called");
            if (subUri[0] != '/')
                subUri = "/" + subUri;

            var adr = _baseAddress + subUri;
            var data = CreatePostPacket(adr, json);

            try
            {
                var res = _cellularRadio.Connect("gpsbox.azurewebsites.net", 80);
                if (!res)
                    return false;
                
                res = _cellularRadio.SendData(data);
                if (!res)
                    return false;

                res = _cellularRadio.Disconnect();
                if (!res)
                    return false;
            }
            catch
            {
                return false;
            }

            return true;
        }

        private const string NewLine = "\r\n";
        private StringBuilder builder = new StringBuilder(500);
        private string CreatePostPacket(string uri, string content)
        {
            builder.Clear();

            builder.Append("POST http://");
            builder.Append(uri);
            builder.Append(" HTTP/1.1");
            builder.Append(NewLine);


            builder.Append("User-Agent: ");
            builder.Append(_userAgent);
            builder.Append(NewLine);

            builder.Append("Host: ");
            builder.Append(_cellularRadio.IpAddress);
            builder.Append(NewLine);

            builder.Append("Content-Type: application/json");
            builder.Append(NewLine);
            
            builder.Append("Content-Length: ");
            builder.Append(content.Length);
            builder.Append(NewLine);
            builder.Append(NewLine);
            builder.Append(content);

            return builder.ToString();
        }
    }
}
